//
//  ReplenishmentVm.swift
//  Pods
//
//  Created by eme on 2017/4/20.
//
//

import UIKit
import UtilCore
import RxSwift
import NetWorkCore
import SwiftyJSON
import AddCarCore
import RickonCore

class ReplenishmentVm: BaseList_Vm {
    
    /*
     接收返回的数据
     */
    /// 返回首页数据
    let productsElements = Variable<[HpProductModel]>([])
    /// 表示正在刷新中
    let refreshing = Variable(false)
    /// 按照品牌商筛选
    let brid_Param =  Variable<String>("")
    ///商品名称搜索
    let pdname_Param =  Variable<String>("")
    
    ///更具pdname获取品牌列表
    let productrapidbrandlist_Subject = PublishSubject<String>()
    /// 返回品牌列表
    let productrapidbrandlist_Success = PublishSubject<[BrlistModel]>()
    ///通过pdid 获取sku 信息
    let glproductskus_Subject = PublishSubject<String>()
    /// 返回sku 信息
    let glproductskus_Success = PublishSubject<ProductModel>()
    
    let addcartsSuccess = PublishSubject<JSON>()
    let addcartsSubject = PublishSubject<NSString>()
    
    init(
        input: (
        refreshTriger: Observable<Void>,
        loadMoreTriger: Observable<Void>
        )
        ) {
        super.init()
        /// 下拉刷新 商品数据列表
        let refreshRequest  = Observable.combineLatest(
            input.refreshTriger.map{ _ in return 0 },
            brid_Param.asDriver().asObservable(),
            pdname_Param.asDriver().asObservable()
        ) { $0}
            .map{ page, brid, pdname  in
                return ReplenishmentApi.productrapidlist(page: page, length: AppConfig.pageSize, brid: brid, pdname: pdname)
            }
            .shareReplay(1)
        /// 下拉刷新发起网络请求
        let refreshResponse = refreshRequest
            .emeRequestApiForArray(HpProductModel.self)
            .shareReplay(1)
        refreshResponse.subscribe(onNext:{ [unowned self] result in
            switch result {
            case .success(let data):
                /**
                 *  如果返回的数据正好相同 则默认还有数据
                 */
                self.hasNextPage.value = data.count == AppConfig.pageSize
                self.productsElements.value = data
                self.page = 1
            case .failure(let error):
                self.error.onNext(error)
            }
            }).addDisposableTo(disposeBag)
        /// 发起加载更多事件源
        let loadMoreRequest = input.loadMoreTriger.map{ _ in
            return ReplenishmentApi.productrapidlist(page: self.page, length: AppConfig.pageSize, brid: self.brid_Param.value, pdname: self.pdname_Param.value)
            }.shareReplay(1)
        /// 加载更多网络请求
        let loadMoreResponse = loadMoreRequest
            .emeRequestApiForArray(HpProductModel.self)
            .shareReplay(1)
        loadMoreResponse.subscribe(onNext:{ [unowned self] result in
            switch result {
            case .success(let data):
                /**
                 *  如果返回的数据正好相同 则默认还有数据
                 */
                self.hasNextPage.value = data.count == AppConfig.pageSize
                self.productsElements.value  += data
                /**
                 *  需要修改page 页数
                 */
                self.page += 1
            case .failure(let error):
                self.error.onNext(error)
            }
        }).addDisposableTo(disposeBag)
        
        /// 刷新数据的进度条
        Observable
            .of(
                refreshRequest.map { _ in true },
                refreshResponse.map { _ in false },
                error.map { _ in false }
            )
            .merge()
            .bindTo(refreshing)
            .addDisposableTo(disposeBag)
        
        /// 加载更多
        Observable
            .of(
                loadMoreRequest.map { _ in true },
                loadMoreResponse.map { _ in false },
                error.map { _ in false }
            )
            .merge()
            .bindTo(refreshing)
            .addDisposableTo(disposeBag)
        /// 通过pdname 获取品牌列表
        productrapidbrandlist_Subject
            .map{ pdname in return ReplenishmentApi.productrapidbrandlist(pdname: pdname)}
            .emeRequestApiForArray(BrlistModel.self)
            .subscribe(onNext:{ [unowned self] result in
                switch result {
                case .success(let data):
                    self.productrapidbrandlist_Success.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).addDisposableTo(disposeBag)
        
        /// 通过pdid 获取sku 信息
        glproductskus_Subject
            .map{ pdid in return HomePageApi.glproductskus(pdid: pdid) }
            .emeRequestApiForObj(ProductModel.self)
            .subscribe(onNext:{ [unowned self] result in
                switch result {
                case .success(let data):
                    self.glproductskus_Success.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).addDisposableTo(disposeBag)
        
        addcartsSubject
            .map{ osCarts in return AddCarApi.addcarts(osCarts: osCarts)}
            .emeRequestApiForRegJson()
            .subscribe(onNext:{ [unowned self] result in
                switch result {
                case .success(let data):
                    self.addcartsSuccess.onNext(data)
                case .failure(let error):
                    self.error.onNext(error)
                }
            }).addDisposableTo(disposeBag)
        
        
    }
    
}
